﻿using System;
using System.Collections.Generic;
using System.Text;

namespace WoWAPI
{
	public partial class Character
	{
		private const string CHARACTER_URL = "http://{0}.battle.net/api/wow/character/{1}/{2}";

		#region Properties
		public int AchievementPoints { get; set; }
		public Classes Class { get; set; }
		#region ClassName
		public string ClassName
		{
			get
			{
				switch (this.Class)
				{
					case Classes.DeathKnight:
						return "Death Knight";
					default:
						return this.Class.ToString();
				}
			}
		}
		#endregion
		public Genders Gender { get; set; }
		public DateTime LastModifiedDate { get { return new DateTime(1970, 1, 1).AddMilliseconds(lastModified); } }
		public long lastModified { get; set; }
		public int Level { get; set; }
		public string Name { get; set; }
		public Races Race { get; set; }
		#region RaceName
		public string RaceName
		{
			get
			{
				switch (this.Race)
				{
					case Races.BloodElf:
						return "Blood Elf";
					case Races.NightElf:
						return "Night Elf";
					default:
						return this.Race.ToString();
				}
			}
		}
		#endregion
		public string Realm { get; set; }
		#region Thumbnail
		public string Thumbnail { get; set; }
		public string ThumbnailFull
		{
			get
			{
				return String.Format("http://{0}.battle.net/static-render/{0}/{1}", this.Region, this.Thumbnail);
			}
		}
		#endregion
		#region ProfileMainFull
		/// <summary>
		/// Note, this is not in blizzard's API, so it must be used sparingly. If used at all, save a local copy to reduce requests to battle.net
		/// </summary>
		public string ProfileMainFull
		{
			get
			{
				return String.Format("http://{0}.battle.net/static-render/{0}/{1}", this.Region, this.Thumbnail.Replace("avatar", "profilemain"));
			}
		}
		#endregion
		#region CardFull
		/// <summary>
		/// Note, this is not in blizzard's API, so it must be used sparingly. If used at all, save a local copy to reduce requests to battle.net
		/// </summary>
		public string CardFull
		{
			get
			{
				return String.Format("http://{0}.battle.net/static-render/{0}/{1}", this.Region, this.Thumbnail.Replace("avatar", "card"));
			}
		}
		#endregion
		public Guild Guild { get; set; }
		public Equipment Items { get; set; }
		public CombatStats Stats { get; set; }
		public Spec[] Talents { get; set; }
		public FactionRep[] Reputation { get; set; }
		public Title[] Titles { get; set; }
		public Skillset Professions { get; set; }
		public Looks Appearance { get; set; }
		public int[] Companions { get; set; }
		public int[] Mounts { get; set; }
		public Pet[] Pets { get; set; }
		public Achievements Achievements { get; set; }
		public Progress Progression { get; set; }
		public string Region { get; set; }
		#endregion

		public Character() { }

		public static Character GetCharacter(string CharacterName, string Realm, string Region, params OptionalFields[] fields)
		{
			string url = String.Format(CHARACTER_URL, Region, Realm, CharacterName);
			
			if (fields.Length > 0)
			{
				StringBuilder sbParams = new StringBuilder();
				foreach (OptionalFields field in fields)
				{
					if (sbParams.Length == 0)
						sbParams.Append("?fields=");
					else
						sbParams.Append(",");

					sbParams.Append(field.ToString().ToLower());
				}
				url += sbParams.ToString();
			}

			Character c = API.DeserializeJSON<Character>(
				API.MakeWebRequest(url).Replace("\"members\":", "\"population\":")); //There is a property conflict in the guild object. When a guild is requested directly, it returns a property "member" of type object array instead of an integer. We're just doing a little switcheroo here to work around it.
			c.Region = Region;

			if (c.Guild != null)
				c.Guild.Region = c.Region;

			return c;
		}

		#region Enums
		public enum Classes
		{
			DeathKnight = 6,
			Druid = 11,
			Hunter = 3,
			Mage = 8,
			Paladin = 2,
			Priest = 5,
			Rogue = 4,
			Shaman = 7,
			Warlock = 9,
			Warrior = 1
		}

		public enum Genders
		{
			Male,
			Female
		}

		public enum Races
		{
			BloodElf = 10,
			Draenei = 11,
			Dwarf = 3,
			Gnome = 7,
			Goblin = 9,
			Human = 1,
			NightElf = 4,
			Orc = 2,
			Tauren = 6,
			Troll = 8,
			Undead = 5,
			Worgen = 22
		}

		public enum OptionalFields
		{
			Guild,
			Stats,
			Talents,
			Items,
			Reputation,
			Titles,
			Professions,
			Appearance,
			Companions,
			Mounts,
			Pets,
			Achievements,
			Progression
		}
		#endregion
	}
}
